package com.wpp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wpp.dao.UserDao;
import com.wpp.domain.User;
import com.wpp.service.UserService;
import com.wpp.util.BCrypt;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * user 服务层实现类
 * @author 吴鹏鹏ppp
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public boolean register(User user) {
        //生成随机盐
        String gensalt = BCrypt.gensalt();
        //加密密码
        String hashpw = BCrypt.hashpw(user.getPassword(), gensalt);
        user.setPassword(hashpw);

        //插入到数据库
        userDao.insert(user);
        return true;
    }

    @Override
    public Map<String, Object> login(User user) {
        //返回的map
        HashMap<String, Object> map = new HashMap<>(2);
        //从数据库查找对应邮箱的user
        User tempUser = new User();
        tempUser.setEmail( user.getEmail() );
        User dbUser = userDao.selectOne(new QueryWrapper<>(tempUser));
        if (dbUser==null){
            map.put("error",true);
            map.put("message","该邮箱未注册");
            return map;
        }
        //效验密码
        boolean checkpw = BCrypt.checkpw(user.getPassword(), dbUser.getPassword());
        if (!checkpw){
            map.put("error",true);
            map.put("message","密码不匹配");
            return map;
        }

        //账号密码正确
        map.put("error",false);
        map.put("user",dbUser);
        return map;
    }


}
